package com.yk.airline.component.analysisdata.impl;

import com.yk.airline.component.analysisdata.api.ICommonAnalysis;
import com.yk.airline.dao.analysisdata.api.IAnalysisDataDao;
import com.yk.airline.dao.origindata.api.IOriginDataDao;
import com.yk.airline.model.AnalysisDataModel;
import com.yk.airline.model.CaptureMarkModel;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.sql.Timestamp;
import java.time.Instant;

/**
 * Project Name:airlinecrawler
 * File Name:TongChengDataAnalysis
 * Package Name:com.yk.airline.component.analysisdata.impl
 * Date:2018/2/1 15:54
 * Author:dengtianjia
 * Description:
 * Copyright (c) 2018, 重庆云凯科技有限公司 All Rights Reserved.
 */
@Service("tongChengDataAnalysis")
public class TongChengDataAnalysis extends CommonAnalysisImpl implements ICommonAnalysis {

    @Resource(name = "analysisDataDao")
    private IAnalysisDataDao analysisDataDao;

    @Override
    public void saveData(Element element , String date ,CaptureMarkModel mark ,String airLine) {
        AnalysisDataModel tempData = new AnalysisDataModel();
        String airCompany = "";
        Double lowestPrice = 0.0;
        String flightNumber = "";
        String flightDate = "";
        String flightOfftime = "";
        String flightArrtime = "";
        Elements selectBaseInfo = element.select(".flist_info .theFlight");
        if(!selectBaseInfo.isEmpty()){
            Element baseInfoEle = selectBaseInfo.get(0);
            airCompany = baseInfoEle.attr("comp");
            tempData.setAirCompany(airCompany);
            lowestPrice = Double.valueOf(baseInfoEle.attr("price"));
            tempData.setLowestPrice(lowestPrice);
            flightNumber = baseInfoEle.attr("fltno");
            tempData.setFlightNumber(flightNumber);
            flightDate = baseInfoEle.attr("flyoffdate");
            flightOfftime = baseInfoEle.attr("offtime");
            flightArrtime = baseInfoEle.attr("arrtime");
            Timestamp offtime = Timestamp.valueOf(flightDate+" "+flightOfftime+":00");
            Timestamp arrtime = Timestamp.valueOf(flightDate+" "+flightArrtime+":00");
            tempData.setDepartureTime(offtime);
            tempData.setArriveTime(arrtime);
        }
        Double specialPrice = 0.0;
        Double preferredPrice = 0.0;
        String preferredInfo = "";
        Elements selectMoreInfo = element.select(".more_flight .theFlight");
        if(!selectMoreInfo.isEmpty()){
            for(Element tempElement : selectMoreInfo){
                Elements tempTd = tempElement.select("td");
                if(tempTd.get(1).text().equals("同程特惠")){
                    specialPrice = Double.valueOf(tempTd.get(6).select("em").get(0).ownText());
                }
                if(tempTd.get(0).text().contains("报销方便")){
                    preferredPrice = Double.valueOf(tempTd.get(6).select("em").get(0).ownText());
                    preferredInfo = tempTd.get(5).text();
                }
            }
        }
        tempData.setSpecialPrice(specialPrice);
        tempData.setPreferredPrice(preferredPrice);
        tempData.setPreferredInfo(preferredInfo);
        tempData.setCreateTime(Timestamp.from(Instant.now()));
        tempData.setUpdateTime(Timestamp.from(Instant.now()));
        tempData.setCaptureMarkByCaptureTime(mark);
        tempData.setAirLine(airLine);
        tempData.setWebsite("同程");
        analysisDataDao.save(tempData);
    }
}
